$(function() {
    var hash = window.location.hash;
    simpleslider.getSlider({
        prop: 'opacity',
        unit: '',
        init: 0,
        show: 1,
        end: 0,
        duration: 1,
        delay: 5
    });
    if (/dw/ig.test(hash)) {
        $('.nav-wrap li').removeClass('active');
        $('.nav-wrap li').eq(1).addClass('active');
        var top = $('#uploadcon').offset().top - 110;
        ScrollTop(top, 300);
    }
    var uploader = WebUploader.create({

        // swf文件路径
        swf: './Uploader.swf',
        method: 'POST',
        // 文件接收服务端。
        server: 'http://www.hefatong.com/document/upload',

        // 选择文件的按钮。可选。
        // 内部根据当前运行是创建，可能是input元素，也可能是flash.
        pick: '#picker',
        accept: {
            title: 'doc',
            extensions: 'doc,docx',
            mimeTypes: 'application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document' //修改位置
        },
        // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
        resize: false
    });
    var state = 'pending';
    uploader.on('fileQueued', function(file) {
        // $list.append( '<div id="' + file.id + '" class="item">' +
        //     '<h4 class="info">' + file.name + '</h4>' +
        //     '<p class="state">等待上传...</p>' +
        // '</div>' );
        $('#filename').text(file.name);
    });

    uploader.on('uploadProgress', function(file, percentage) {
        //console.log('上传中');
        popup('合同智能分析中');
    });

    uploader.on('uploadError', function(file, reason) {
        //alert('上传出错');
        //请重新上传您的文档
        popup('请重新上传您的文档');
    });


    uploader.on('uploadSuccess', function(file, response) {
        //上传成功
        if (response.code === 200) {
            // popup('上传成功!即将跳转到审核结果页');
            //  window.setTimeout(function(){
            //     window.location.href = '/result/'+response.document;
            // }, 2000);
            $.ajax({
                url: 'http://www.hefatong.com/contract/diagnose',
                type: 'get',
                data: {
                    filename: response.document
                },
                success: function(data) {
                    var d = JSON.parse(data);
                    if (d.code === 200) {
                        popup('上传成功!即将跳转到审核结果页');
                        window.setTimeout(function() {
                            window.location.href = '/result/' + response.document;
                        }, 2000);
                    } else {
                        popup(d.message);
                        window.setTimeout(function() {
                            closePopup();
                        }, 5000)
                    }

                },
                error: function(err) {
                    alert('网络错误!');
                }
            });
        } else {
            popup(response.message);
        }
        // popup('上传成功!即将跳转到审核结果页');

        // window.setTimeout(function(){
        //     //window.location.href = '/result';
        // }, 2000)
    });
    $('#ctlBtn').on('click', function() {
        closePopup();
        uploader.upload();
        // $.ajax({
        //     url:'http://open.sdlvyue.com/gateway.htm?request_id=1516069968574&app_id=cbi1513925261350&timeStamp=1516069968636&version=1.0&biz_content=75d3e6a9af6c52fada51b6b64face5ab&sign=',
        //     type:'post',
        //     success:function(data){

        //     }
        // });
        // axios({
        //     method: 'post',
        //     url: 'http://open.sdlvyue.com/gateway.htm?request_id=1516069968574&app_id=cbi1513925261350&timeStamp=1516069968636&version=1.0&biz_content=75d3e6a9af6c52fada51b6b64face5ab&sign=',
        // }).then(function(response){
        //     console.log(response);
        // }).catch(function (error) {
        //     console.log(error);
        // });
    });

    function ScrollTop(number, time) {
        var number = number ? number : 0;
        if (!time) {
            document.body.scrollTop = document.documentElement.scrollTop = number;
            return number;
        }
        var spacingTime = 20; // 设置循环的间隔时间  值越小消耗性能越高
        var spacingInex = time / spacingTime; // 计算循环的次数
        var nowTop = document.body.scrollTop + document.documentElement.scrollTop; // 获取当前滚动条位置
        var everTop = (number - nowTop) / spacingInex; // 计算每次滑动的距离
        var scrollTimer = setInterval(function() {
            if (spacingInex > 0) {
                spacingInex--;
                ScrollTop(nowTop += everTop);
            } else {
                clearInterval(scrollTimer); // 清除计时器
            }
        }, spacingTime);
    }
    $('#tab-upload').on('click', function() {
        $('.nav-wrap li').removeClass('active');
        $(this).addClass('active');
        var top = $('#uploadcon').offset().top - 110;
        ScrollTop(top, 300);
    });
    //弹框
    function popup(msg) {
        closePopup();
        var tpl = [
            '<div id="popup" class="popup-wrap">',
            '<p class="popup-title">提示</p>',
            '<p class="popup-msg">' + msg + '</p>',
            '</div>',
            '<div id="mask" class="popup-mask"></div>',
        ];
        $('body').append(tpl.join(''));

    }

    function closePopup() {
        $('#popup, #mask').remove();
    }
});